package com.bgs.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bgs.pojo.*;
import com.bgs.pojo.data.stuInfoData;
import com.bgs.pojo.vo.ClassroomVO;
import com.bgs.pojo.vo.studentInformationVO;
import com.bgs.pojo.vo.uVO;
import com.bgs.service.ClassService;
import com.bgs.service.ERegisterService;
import com.bgs.service.StudenService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 梁玉杰
 * @Date: 2022/3/21 16:54
 * @Description: TODO
 */
@RestController
@RequestMapping("/classVue")
@Log4j2
@CrossOrigin
public class ClassVueController {


    @Autowired
    private ClassService cS;


    //全查+模糊差------新技术（因为需要返回map集合，但是呢，不推荐用map）
    @RequestMapping("/classfindAllormodil")
//    public R StudentfindAllormodil(@RequestBody(required = false) studentInformationVO sVO){
    public R StudentfindAllormodil(@RequestBody(required = false) ClassroomVO cVo){

        //模糊查询前端传过来的数据
        log.info("模糊查询前端传过来的数据ClassroomVO"+cVo);

        //调用分页查询账单类列表的方法
        PageHelper.startPage(cVo.getPage(),cVo.getLimit());
//        studentInformationVO svo = new studentInformationVO();
        List<Classroom> CS = cS.findAllandmohu(cVo);
        //创建分页对象
        PageInfo<Classroom> pageInfo = new PageInfo<Classroom>(CS);
        return  R.ok().data("total",pageInfo.getTotal()).data("rows",pageInfo.getList());
    }


    @GetMapping("/threexlk")
    public R threexlk(){
        List<Classroom> xlk = cS.findAllxlk();
        log.info("下拉框的1111"+xlk);
        return R.ok().data("items",xlk);    }

    @PostMapping("/threexlkbn")
    public R threexlkbn(@RequestBody ClassroomVO classroom){
        log.info("下拉框的22222"+classroom);
        List<Classroom> xlk = cS.findAllxlkbn(classroom);
        return R.ok().data("items",xlk);    }

    @PostMapping("/threexlkbf")
    public R threexlkbf(@RequestBody ClassroomVO classroom){
        log.info("下拉框的33333"+classroom);
        List<Classroom> xlk = cS.findAllxlkbf(classroom);
        return R.ok().data("items",xlk);
    }


    /**
     * @Author 梁玉杰
     * @Description class的添加
     * @Date 2021/7/27 17:07
     * @Param
     * @return
     **/
    @PostMapping("/addClass")
    public R addClass (@RequestBody Classroom c){
        HashMap<String, Object> m = new HashMap<>();
        log.info("添加的"+c);
        boolean flag = cS.insertSelective(c) > 0;
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("/getstu/{id}")
    public R getTeacher(@PathVariable String id){
        Classroom c =  cS.selectByPrimaryKey(Integer.valueOf(id));
        return R.ok().data("teacher",c);
    }

    //修改
    @PostMapping("/updClass")
    public R updClass(@RequestBody ClassroomVO cVO){
        Map<String,Object> m = new HashMap<>();
        log.info("修改的VO"+cVO);
        boolean flag = (cS.updateByPrimaryKeySelective(cVO) > 0);
            if (flag){
                return R.ok();
            }else {
                return R.error();
            }
    }


    /**
     * 删除学生
     */
    @RequestMapping("/deleteBypkId/{stuId}")
    public R deleteBypkId(@PathVariable Integer pkId){
        Map<String,Object> m = new HashMap<>();
        if(cS.deleteById(pkId)>0){
            m.put("success",true);
            m.put("message","删除成功");
        }else{
            m.put("success",false);
            m.put("message","删除失败");
        }
        boolean flag =(cS.deleteById(Integer.valueOf(pkId))>0);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 批量删除账单  前端是数组
     */
    @DeleteMapping("/batchDeleteClass/{ids}")
    public R batchDeleteClass(@PathVariable Integer[] ids){
                int count = 0;
//        System.out.println("前端传入的数组：："+ids);
        for (int i = 0; i < ids.length; i++) {
//            System.out.println("前端传入的数组：："+ids[i]);
            int a = cS.deleteById(Integer.valueOf(ids[i]));
            if(a>0){
                count++;
            }
        }
        //判断受影响行数是否为0
        if (count>0){
            return R.ok();
        }else {
            return R.error();
        }
    }


//    //下载
//    @GetMapping("/read")
//    public R read(){
//        List<stuInfoData> alist = studenService.select();//全查
////        log.info("aaaaaaaaa"+alist);
//        String fileName = "D:\\excel\\stu.xlsx";
//        EasyExcel.write(fileName,stuInfoData.class).sheet("学生列表").doWrite(alist);
//        return R.ok();
//    }
//
//
////    添加课程分类
////    获取上传过来的文件，把文件内容读取出来
//    @PostMapping("/addSubject")
//    public R addSubject(MultipartFile file){
//        //上传文件过来的Excel文件 上传固定的地址
////        eduSubjectService.addSubject(file,eduSubjectService);
////        String fileName = "D:\\excel\\stu.xlsx";
////        studenService.addStuExel(fileName,studenService);
//        //动态地址
//        studenService.addStuExel(file,studenService);
//        return R.ok();
//    }
//
//
//
////    @GetMapping("/readuu")
////    public R readuu(){
////        List<Userte> alist = studenService.selectuu();
////        log.info("aaaaaaaaa"+alist);
////        String fileName = "D:\\excel\\user.xlsx";
////        EasyExcel.write(fileName,Userte.class).sheet("学生列表").doWrite(alist);
////        return R.ok();
////    }
////    //    添加课程分类
//////    获取上传过来的文件，把文件内容读取出来
////    @GetMapping("/addUser")
////    public R addUser(){
////        //上传文件过来的Excel文件
//////        eduSubjectService.addSubject(file,eduSubjectService);
////        String fileName = "D:\\excel\\user.xlsx";
////
////        studenService.addUser(fileName,studenService);
////        return R.ok();
////    }



}
